package com.basswold.nasaesa.client;

public class Module implements Cloneable{

	private boolean usable;
	private String type;
	private String condition;
	private String orientation;
	private int id = -1;
	private int currx, curry;
	public static String condavail[] = {
		"undamaged", "uncertain", "damaged"};

	public static String orientavail[] = {
		"0", "1", "2"};

	public static String typeavail[] = {
		"Plain", "Dormitory", "Sanitation", "Food", "Gym", "Canteen", "Power", "Control", "Airlock", "Medical"};
	
	public Module(int x, int y, int newid, String condition, String orientation) {
		currx = x;
		curry = y;
		checkUsable();
		setId(newid);
		setCondition(condition);
		setOrientation(orientation);
	}

	public Module(int newid, String condition, String orientation) {
		setId(newid);
		setCondition(condition);
		setOrientation(orientation);
	}

	public Module(int x, int y, int newid) {
		currx = x;
		curry = y;
		checkUsable();
		setId(newid);
		setCondition("good");
		setOrientation("0");
	}

	public boolean setId(int newId) {
		boolean snglcheck = false;
		if (check_id(newId)) {
			snglcheck = true;
			this.id = newId;
		}
		return snglcheck;
	}

	public boolean setType(String descriptor) {
		boolean good = false;
		for(int i = 0; i < typeavail.length ; i ++){
			if (typeavail[i].equals(descriptor)) {
				type = descriptor;
				good = true;
			}
		}
		return good;
	}

	public boolean setCondition(String descriptor) {
		boolean good = false;
		for(int i = 0; i < condavail.length ; i ++){
			if (condavail[i].equals(descriptor) == true) {
				condition = descriptor;
				good = true;
				if (descriptor.equals(condavail[2])){
					usable = false;
				}
			}
		}
		return good;
	}

	public boolean setOrientation(String descriptor) {
		boolean good = false;
		for(int i = 0; i < orientavail.length ; i ++){
			if (orientavail[i].equals(descriptor)) {
				orientation = descriptor;
				good = true;
			}
		}
		return good;
	}

	public void setCurrX(int x) {
		currx = x;
	}

	public void setCurrY(int y) {
		curry = y;
	}
	
	public void checkUsable(){
		if (((currx >= 40) && (currx <=50)) || ((curry >= 40) && (curry <=50))){
			usable = false;
		}
	}
	
	public boolean getUsable(){
		return usable;
	}
	
	public String getType() {
		return type;
	}

	public String getCondition() {
		return condition;
	}

	public String getOrientation() {
		return orientation;
	}

	public int getId() {
		return id;
	}

	public int getCurrX() {
		return currx;
	}

	public int getCurrY() {
		return curry;
	}

	private boolean check_id(int id_num) {
		boolean dbcheck = false;
		if ((id_num >= 1) && (id_num <= 40)) {
			dbcheck = setType("Plain");
		} else if ((id_num >= 61) && (id_num <= 80)) {
			dbcheck = setType("Dormitory");
		} else if ((id_num >= 91) && (id_num <= 100)) {
			dbcheck = setType("Sanitation");
		} else if ((id_num >= 111) && (id_num <= 120)) {
			dbcheck = setType("Food");
		} else if ((id_num >= 131) && (id_num <= 134)) {
			dbcheck = setType("Gym");
		} else if ((id_num >= 141) && (id_num <= 144)) {
			dbcheck = setType("Canteen");
		} else if ((id_num >= 151) && (id_num <= 154)) {
			dbcheck = setType("Power");
		} else if ((id_num >= 161) && (id_num <= 164)) {
			dbcheck = setType("Control");
		} else if ((id_num >= 171) && (id_num <= 174)) {
			dbcheck = setType("Airlock");
		} else if ((id_num >= 181) && (id_num <= 184)) {
			dbcheck = setType("Medical");
		}
		return dbcheck;
	}

	public static String id_to_type(int id_num){
		String dbcheck = "";
		if ((id_num >= 1) && (id_num <= 40)) {
			dbcheck = "Plain";
		} else if ((id_num >= 61) && (id_num <= 80)) {
			dbcheck = "Dormitory";
		} else if ((id_num >= 91) && (id_num <= 100)) {
			dbcheck = "Sanitation";
		} else if ((id_num >= 111) && (id_num <= 120)) {
			dbcheck = "Food";
		} else if ((id_num >= 131) && (id_num <= 134)) {
			dbcheck = "Gym";
		} else if ((id_num >= 141) && (id_num <= 144)) {
			dbcheck = "Canteen";
		} else if ((id_num >= 151) && (id_num <= 154)) {
			dbcheck = "Power";
		} else if ((id_num >= 161) && (id_num <= 164)) {
			dbcheck = "Control";
		} else if ((id_num >= 171) && (id_num <= 174)) {
			dbcheck = "Airlock";
		} else if ((id_num >= 181) && (id_num <= 184)) {
			dbcheck = "Medical";
		}
		return dbcheck;
	}

	public String toString(){
		return id + ": at ("+currx+","+curry+") "+condition+" "+orientation;
	}
	
	public String storageString(){
		return "{"+id+":"+(currx-1)+":"+(curry-1)+":"+condition+":"+orientation+"}";
	}
	
	public String getIDXY(){
		return "{"+id+":"+currx+":"+curry+"}";
	}
}
